package com.canbot.u05.adapter;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.canbot.u05.R;
import com.canbot.u05.bean.CustomDialogue;

import java.util.List;

/**
 * @author liuyusheng
 * @ClassName: ListViewAdapter
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @date 2016年7月22日 下午3:04:49
 */
public class ListViewAdapter extends BaseAdapter {

        private ViewHolder holder;

        private List<CustomDialogue> mList;

        private int mMinItemWidth;

        private int mMaxItemWidth;

        private Context context;

        private AnimationDrawable anim;

        private LayoutInflater mInflater;

        private List<String> mP_id;


        public ListViewAdapter(Context context, List<CustomDialogue> list) {
                this.context = context;
                this.mList = list;
                //下面是根据屏幕宽度初始化一个最大宽度和一个最小宽度（控制录音条的长度）
                WindowManager windowManager = (WindowManager) context
                        .getSystemService(Context.WINDOW_SERVICE);
                DisplayMetrics outMetrics = new DisplayMetrics();
                windowManager.getDefaultDisplay().getMetrics(outMetrics);
                mMaxItemWidth = (int) (outMetrics.widthPixels * 0.8f);    //控制录音条的最大宽度
                mMinItemWidth = (int) (outMetrics.widthPixels * 0.15f);    //控制录音条的最小宽度
        }

        @Override
        public int getCount() {
                return mList.size();
        }

        @Override
        public Object getItem(int position) {
                return null;
        }

        @Override
        public long getItemId(int position) {
                return 0;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
                if (convertView == null) {
                        holder = new ViewHolder();
                        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                        convertView = mInflater.inflate(
                                R.layout.listview_item, null);
                        holder.betxt_show = (TextView) convertView.findViewById(R.id.tv_show);
                        holder.iv_delete = (ImageView) convertView.findViewById(R.id.iv_delete);
                        holder.iv_play = (ImageView) convertView.findViewById(R.id.iv_playMusic);

                        convertView.setTag(holder);
                }
                else {
                        holder = (ViewHolder) convertView.getTag();
                }
                final View mView;
                mView = convertView;
                holder.betxt_show.setTag(mList.get(position).getQuestion());
                holder.betxt_show.setText("" + mList.get(position).getQuestion());
                holder.iv_play.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                                if (mOnClickListener != null) {
                                        mOnClickListener.onItemAnimClick(position, mView);
                                }

                        }
                });
                holder.iv_delete.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                                if (mOnClickListener != null) {
                                        mOnClickListener.onItemdeleteClick(position, mView);
                                }

                        }
                });
                return convertView;
        }

        class ViewHolder {

                TextView betxt_show;

                ImageView iv_delete;

                ImageView iv_play;
        }

        interface playUrl {

                void playurl();
        }


        private OnIClickListener mOnClickListener;

        public void setOnClickListener(OnIClickListener l) {
                mOnClickListener = l;
        }

        public interface OnIClickListener {

                void onItemimgOKClick(int position, View view);

                void onItemAnimClick(int postion, View view);

                void onItemdeleteClick(int postion, View view);

        }


}
